package com.springboot.base.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author beike
 * @date 2021/3/4
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {
//    @Autowired
//    private TokenService tokenService;

//    @Autowired
//    public boolean preHandle(HttpServletRequest request,
//                             HttpServletResponse response, Object handler) throws Exception {
//        // 如果不是映射到方法直接通过
//        if (!(handler instanceof HandlerMethod)) {
//            return true;
//        }
//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        Method method = handlerMethod.getMethod();
//
//        // 判断接口是否需要登录
//        LoginRequired methodAnnotation = method.getAnnotation(LoginRequired.class);
//        // 有 @LoginRequired 注解，需要认证
//        if (methodAnnotation != null) {
//            // 执行认证
//            String token = request.getHeader("token");  // 从 http 请求头中取出 token
//            if (StringUtils.isEmpty(token)) {
//                token = request.getParameter("token");
//            }
//            int tokenLength = 32;
//            if (StringUtils.isEmpty(token)) {
//                throw new ServiceException(ResultCode.USER_NOT_LOGIN);
//            } else if (token.length() != tokenLength) {
//                throw new ServiceException(ResultCode.INVALID_TOKEN);
//            } else {
//                boolean tokenStatus = tokenService.verifyToken(token);
//                if (!tokenStatus) {
//                    throw new ServiceException(ResultCode.LOGIN_EXPIRED);
//                }
//            }
//            return true;
//        }
//        return true;
//
//    }

    /**
     * 请求处理之后
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

    /**
     * 请求处理之后调用
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }


    /**
     * 请求处理之前调用
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}